<template>
  <div class="logic-wrap tag-scrollbar">
    <template v-if="derivedTag.tagList.length || drillGrps.length">
      <template v-if="derivedTag.tagList.length">
        <p class="sub-title"><icon-svg name="iconSystem"></icon-svg>原始客群逻辑</p>
        <div class="ul">
          <div
            class="logical"
            :class="{ 'logical-or': derivedTag.logicalOperation === 'or' }"
            v-if="derivedTag.tagList.length > 1"
          >
            <el-select v-model="derivedTag.logicalOperation" disabled popper-class="factory-select" class="default">
              <el-option label="且" value="and"></el-option>
              <el-option label="或" value="or"></el-option>
            </el-select>
          </div>
          <div v-for="(item, index) in derivedTag.tagList" :key="index" class="ul-wrap">
            <!-- 没有下级 -->
            <template v-if="!item.tagList">
              <div class="list-item" :level="1" :tagId="item.tagId" :index="index">
                <el-row :gutter="8">
                  <el-col :span="8">
                    <div class="item" :class="{ 'lost-item': !item.tagId }" align="center">
                      {{ item.tagNm }}
                    </div>
                  </el-col>
                  <template v-if="item.tagId">
                    <el-col :span="4" align="center">
                      <div class="item">
                        <el-select v-model="item.operationType" disabled>
                          <el-option label="---" value=""></el-option>
                          <el-option
                            v-for="opera in operatorType('type' + item.valueType)"
                            :key="opera.value"
                            :label="opera.key"
                            :value="opera.value"
                          ></el-option>
                        </el-select>
                      </div>
                    </el-col>
                    <el-col :span="12">
                      <div class="item">
                        <factory-content-item :config="item" disabled></factory-content-item>
                      </div>
                    </el-col>
                  </template>
                  <el-col :span="16" v-else>
                    <div class="item lost-item">标签被更改或删除</div>
                  </el-col>
                </el-row>
              </div>
            </template>
            <template v-else>
              <div class="ol" :class="{ space: item.tagList.length > 1 }">
                <div class="logical" :class="{ 'logical-or': item.logicalOperation === 'or' }">
                  <el-select v-model="item.logicalOperation" popper-class="factory-select" disabled class="default">
                    <el-option label="且" value="and"></el-option>
                    <el-option label="或" value="or"></el-option>
                  </el-select>
                </div>
                <div v-for="(child, index2) in item.tagList" :key="index2">
                  <!-- 没有第三层 -->
                  <template v-if="!child.tagList">
                    <div class="list-item">
                      <el-row :gutter="8">
                        <el-col :span="8">
                          <div class="item" :class="{ 'lost-item': !child.tagId }" align="center">
                            {{ child.tagNm }}
                          </div>
                        </el-col>
                        <template v-if="child.tagId">
                          <el-col :span="4" align="center">
                            <div class="item">
                              <el-select v-model="child.operationType" disabled>
                                <el-option label="---" value=""></el-option>
                                <el-option
                                  v-for="opera in operatorType('type' + child.valueType)"
                                  :key="opera.value"
                                  :label="opera.key"
                                  :value="opera.value"
                                ></el-option>
                              </el-select>
                            </div>
                          </el-col>
                          <el-col :span="12">
                            <div class="item">
                              <factory-content-item :config="child" disabled></factory-content-item>
                            </div>
                          </el-col>
                        </template>
                        <el-col :span="16" v-else>
                          <div class="item lost-item">标签被更改或删除</div>
                        </el-col>
                      </el-row>
                    </div>
                  </template>
                  <div class="ol space" v-else>
                    <!-- 第三层的逻辑符号 -->
                    <div class="logical" :class="{ 'logical-or': child.logicalOperation === 'or' }">
                      <el-select
                        v-model="child.logicalOperation"
                        popper-class="factory-select"
                        disabled
                        class="default"
                      >
                        <el-option label="且" value="and"></el-option>
                        <el-option label="或" value="or"></el-option>
                      </el-select>
                    </div>
                    <div v-for="(third, index3) in child.tagList" :key="index3">
                      <div class="list-item">
                        <el-row :gutter="8">
                          <el-col :span="8">
                            <div class="item" :class="{ 'lost-item': !third.tagId }" align="center">
                              {{ third.tagNm }}
                            </div>
                          </el-col>
                          <template v-if="third.tagId">
                            <el-col :span="4" align="center">
                              <div class="item">
                                <el-select v-model="third.operationType" disabled>
                                  <el-option label="---" value=""></el-option>
                                  <el-option
                                    v-for="opera in operatorType('type' + third.valueType)"
                                    :key="opera.value"
                                    :label="opera.key"
                                    :value="opera.value"
                                  ></el-option>
                                </el-select>
                              </div>
                            </el-col>
                            <el-col :span="12">
                              <div class="item">
                                <factory-content-item :config="third" disabled></factory-content-item>
                              </div>
                            </el-col>
                          </template>
                          <el-col :span="16" v-else>
                            <div class="item lost-item">标签被更改或删除</div>
                          </el-col>
                        </el-row>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </template>
          </div>
        </div>
      </template>
      <div class="no-record" v-else>
        <img src="~@/assets/img/no_records.png" /><br />
        暂无原始客群逻辑
      </div>
      <div class="drill-wrap" v-if="drillGrps.length">
        <div v-for="(item, index) in drillGrps" :key="index" class="drill-content">
          <p class="sub-title"><icon-svg name="iconDrill"></icon-svg>{{ index + 1 }}级下钻逻辑</p>
          <div class="drill-logic" v-if="item.length > 1">
            <span>或</span>
          </div>
          <template v-for="drill in item">
            <div :key="drill.seriesId" class="drill-item-wrap">
              <div class="drill-logic" v-if="drill.subDrill && drill.subDrill.seriesId"><span>且</span></div>
              <div class="drill-item" :class="{ 'sub-drill': drill.subDrill && drill.subDrill.seriesId }">
                <el-row :gutter="20">
                  <el-col :span="8">
                    <div class="drill-name">{{ drill.mainDrill.tagNm }}</div>
                  </el-col>
                  <el-col :span="1" align="center" class="drill-sybmol">：</el-col>
                  <el-col :span="15">
                    <div class="drill-value">{{ drill.mainDrill.name }}</div>
                  </el-col>
                </el-row>
              </div>
              <div class="drill-item sub-drill" v-if="drill.subDrill && drill.subDrill.seriesId">
                <el-row :gutter="20">
                  <el-col :span="8">
                    <div class="drill-name">{{ drill.subDrill.tagNm }}</div>
                  </el-col>
                  <el-col :span="1" align="center" class="drill-sybmol">：</el-col>
                  <el-col :span="15">
                    <div class="drill-value">{{ drill.subDrill.name }}</div>
                  </el-col>
                </el-row>
              </div>
            </div>
          </template>
        </div>
      </div>
    </template>
    <div class="no-record" v-else>
      <img src="~@/assets/img/no_records.png" /><br />
      暂无内容
    </div>
  </div>
</template>

<script>
import operator from '@/views/modules/factory/operator'
import factoryContentItem from '@/views/modules/factory/factory-content-item'
export default {
  components: { factoryContentItem },
  props: {
    derivedTag: {
      type: Object,
      required: true,
      default: () => {}
    },
    drillGrps: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {}
  },
  methods: {
    operatorType(key) {
      return operator[key]
    }
  }
}
</script>

<style lang="scss">
.logic-wrap {
  padding: 20px;
  height: calc(100% - 60px);
  background-color: #f6f7f8;
  overflow-y: auto;
  .no-record {
    text-align: center;
    img {
      width: 200px;
      margin-bottom: 10px;
    }
  }
  .ul,
  .ol {
    padding-left: 100px;
    position: relative;
    &.space {
      padding-left: 100px;
    }
    .space-tag {
      span {
        width: 80px;
        height: 40px;
        text-align: center;
        line-height: 40px;
        color: #fff;
        background-color: #479fa3;
        position: absolute;
        top: 50%;
        left: 0;
        margin: -20px 0 0 10px;
        border-radius: 20px;
        z-index: 1;
      }
      &:before {
        width: 3px;
        content: '';
        background-color: #479fa3;
        position: absolute;
        top: 0;
        bottom: 50%;
        left: 50px;
      }
      &:after {
        width: 3px;
        content: '';
        background-color: #479fa3;
        position: absolute;
        top: 50%;
        bottom: 0;
        left: 50px;
      }
    }
    .logical {
      width: 100px;
      position: absolute;
      top: 0;
      left: 0;
      bottom: 0;
      &.space-logical {
        left: 100px;
      }
      .el-select {
        position: absolute;
        top: 50%;
        left: 15px;
        margin-top: -20px;
      }
      .el-input__suffix {
        display: none;
      }
      .el-input__inner {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        padding: 0 !important;
        text-align: center;
        color: #fff !important;
        background-color: #bf843e;
      }
      &:before {
        width: 60%;
        height: calc(50% - 30px);
        content: '';
        border-width: 3px;
        border-style: solid;
        border-color: transparent;
        border-top-color: #bf843e;
        border-left-color: #bf843e;
        position: absolute;
        top: 20px;
        bottom: 50%;
        right: calc(10% - 5px);
      }
      &:after {
        width: 60%;
        height: calc(50% - 30px);
        content: '';
        border-width: 3px;
        border-style: solid;
        border-color: transparent;
        border-bottom-color: #bf843e;
        border-left-color: #bf843e;
        position: absolute;
        top: calc(50% + 10px);
        bottom: 20px;
        right: calc(10% - 5px);
      }
      &.logical-or {
        &:before {
          border-top-color: #479fa3;
          border-left-color: #479fa3;
        }
        &:after {
          border-bottom-color: #479fa3;
          border-left-color: #479fa3;
        }
        .el-input__inner {
          background-color: #479fa3;
        }
      }
    }
  }
  .ul-wrap {
    margin: 5px;
    border: 1px solid #ddd;
    box-shadow: 1px 1px 5px #ddd;
  }
  .list-item {
    padding: 10px 50px 10px 10px;
    margin-bottom: 20px;
    border-radius: 5px;
    // background-color: #fff;
    position: relative;
  }
  .item {
    height: 40px;
    line-height: 40px;
    color: #455fbc;
    background-color: #c0d2f6;
    border-radius: 5px;
  }
  .lost-item {
    text-align: center;
    color: #fff !important;
    background-color: #ff7c7c !important;
    border-color: #ff7c7c !important;
  }
  .el-col-8 .item {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .el-button {
    color: #455fbc;
    background-color: #c0d2f6;
    border-color: #c0d2f6;
    position: absolute;
    right: 10px;
    top: 15px;
  }
  .el-input__prefix,
  .el-input__suffix,
  .el-date-editor .el-range__icon,
  .el-select .el-input .el-select__caret {
    color: #455fbc;
  }
  .el-tag.el-tag--info {
    color: #4455bb;
    background-color: #e8f0ff;
  }
  .el-select .el-tag__close.el-icon-close {
    color: #fff;
    background-color: #97b5f3;
  }
  .el-input__inner,
  .el-range-editor .el-range-input,
  .el-select__input {
    color: #455fbc !important;
    background-color: #c0d2f6;
    border: 0;
    &::-webkit-input-placeholder {
      color: #455fbc;
    }
    &::-moz-placeholder {
      color: #455fbc;
    }
    &::-ms-input-placeholder {
      color: #455fbc;
    }
  }
  .el-select,
  .el-input-number,
  .el-date-editor {
    width: 100%;
    &.normal {
      width: 25%;
    }
  }
  .el-input-number .el-input__inner {
    padding: 0 15px;
    text-align: left;
  }
  .el-input-number__decrease,
  .el-input-number__increase {
    display: none;
  }
  .el-date-editor {
    height: auto;
    .el-range-input {
      color: #455fbc;
    }
    .el-range-separator {
      color: #455fbc;
      width: 30px;
    }
  }

  .el-col-4 {
    .el-input__inner {
      color: #4455bb;
      text-align: center;
    }
    .el-input__inner,
    .el-range-editor .el-range-input,
    .el-select__input {
      background-color: #fff;
      border: 1px solid #eee;
    }
  }
  .factory-content-item {
    background-color: #f6f7f8;
    .el-input__inner,
    .el-range-editor .el-range-input,
    .el-select__input {
      background-color: #fff;
      border: 1px solid #eee;
    }
    .el-select__tags .el-select__input,
    .el-date-editor .el-range-input {
      border: 0;
    }
  }
  .drill-wrap {
    padding: 0 20px 10px 20px;
    background-color: #f6f7f8;
    border-top: 1px dashed $border-color;
  }
  .drill-content {
    margin: 10px 0 20px 0;
    position: relative;
  }
  .sub-title {
    font-size: 16px;
    margin-bottom: 20px;
    svg {
      width: 20px;
      height: 20px;
      color: $theme-color;
      margin: -2px 10px 0 0;
      vertical-align: middle;
    }
  }
  .drill-item-wrap {
    padding: 10px 25px;
    margin-left: 80px;
    margin-bottom: 20px;
    border: 1px solid #ddd;
    box-shadow: 1px 1px 5px #ddd;
    position: relative;
    .drill-logic {
      left: 20px;
      span {
        top: calc(50% - 30px);
        background-color: #bf843e;
      }
      &:before {
        top: 20px;
        border-top-color: #bf843e;
        border-left-color: #bf843e;
      }
      &:after {
        height: calc(50% - 45px);
        border-bottom-color: #bf843e;
        border-left-color: #bf843e;
      }
    }
  }
  .drill-item {
    height: 40px;
    line-height: 40px;
    margin-bottom: 20px;
  }
  .drill-name {
    display: inline-block;
    width: 100%;
    color: #455fbc;
    background-color: #c0d2f6;
    border-radius: 5px;
    text-align: center;
  }
  .drill-value {
    padding: 0 20px;
    border: 1px solid #c0d2f6;
    color: #455fbc;
    background-color: #fff;
    border-radius: 4px;
  }
  .drill-sybmol {
    font-size: 25px;
    font-weight: bolder;
    color: $theme-color;
  }
  .sub-drill {
    margin-left: 100px;
  }
  .drill-logic {
    width: 100px;
    position: absolute;
    top: 5px;
    bottom: 0;
    left: 0;
    span {
      display: inline-block;
      width: 40px;
      height: 40px;
      text-align: center;
      line-height: 40px;
      color: #fff;
      background-color: #479fa3;
      border-radius: 50%;
      position: absolute;
      left: 0;
      top: 50%;
      z-index: 2;
    }
    &::before {
      width: 60%;
      height: calc(50% - 30px);
      content: '';
      border-width: 3px;
      border-style: solid;
      border-color: transparent;
      border-top-color: #479fa3;
      border-left-color: #479fa3;
      position: absolute;
      top: 60px;
      bottom: 50%;
      right: 20px;
    }
    &:after {
      width: 60%;
      height: calc(50% - 30px);
      content: '';
      border-width: 3px;
      border-style: solid;
      border-color: transparent;
      border-bottom-color: #479fa3;
      border-left-color: #479fa3;
      position: absolute;
      top: calc(50% + 8px);
      bottom: 20px;
      right: 20px;
    }
  }
}
</style>
